package control;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import model.Zuzhi;
import util.Pagination;
import db.DBConnect;

public class ZuzhiControl {
	public static Pagination pageInstance = null;
	
	/**
	 * 增加
	 * @author 
	 * @return
	 * 2012-02-23
	 */
	public boolean insertZuzhi(Zuzhi zuzhi)
	{
		DBConnect dbc = new DBConnect();
		String sql ="insert into zuzhi (jingpName,jingptu,jingpNeirong) values (?,?,?)";
		
		dbc.prepareStatement(sql);
		
		try {
			dbc.setString(1, zuzhi.getJingpName());
			dbc.setString(2, zuzhi.getJingptu());
			dbc.setString(3, zuzhi.getJingpNeirong());
					
			dbc.executeUpdate();
			return true;
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}finally {
			dbc.close();
		}
	}
	
	/*判断数据库里面是否存在用户*/
	public static boolean isZuzhi(String a,String b,String c)
	{
		DBConnect dbc = new DBConnect();
		String sql ="select * from zuzhi where jingpname = ? and jingptu=? and jingpNeirong=?";
		dbc.prepareStatement(sql);
		try {
			dbc.setString(1,a);
			dbc.setString(2,b);
			dbc.setString(3,c);
			ResultSet rs= dbc.executeQuery();
			if(rs.next()){
				return true;
			}
			else{
				return false;
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			dbc.close();
		}return false;
	}
	
	
	/**
	 * 根据id获取信息
	 * @author dujuan
	 * 2012-02-23
	 */
	
	public Zuzhi GetZuzhi(int id)
	{
		DBConnect dbc = new DBConnect();
		String sql = "select * from zuzhi where jingpId = ?";

		dbc.prepareStatement(sql);
		
		try {
			dbc.setInt(1, id);
			ResultSet rs = dbc.executeQuery();
			Zuzhi zuzhi = new Zuzhi();
			
			if(rs.next())
			{
				zuzhi.setJingpName(rs.getString("jingpName"));
				zuzhi.setJingptu(rs.getString("jingptu"));
				zuzhi.setJingpNeirong(rs.getString("jingpNeirong"));
						
				return zuzhi;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			dbc.close();
		}
		return null;
	}
	
	
	/**
	 * 删除
	 * @author dujuan
	 * 2012-02-23
	 */
	
	public boolean deleteZuzhi(int id)
	{
		DBConnect dbc = new DBConnect();
		String sql = "delete from zuzhi where jingpId =?";
		
		dbc.prepareStatement(sql);
		
		try {
			dbc.setInt(1, id);
			dbc.executeUpdate();
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}finally{
			dbc.close();
		}
		
	}
	
	/**
	 * 修改
	 * @author dujuan
	 * 2012-02-23
	 */
	
	public boolean updateZuzhi (Zuzhi zuzhi, int id)
	{
		DBConnect dbc = new DBConnect();
		String sql = "update zuzhi set jingpName=?, jingptu=? ,jingpNeirong=? where jingpId =?";
		dbc.prepareStatement(sql);
		
		try {
			dbc.setString(1, zuzhi.getJingpName());
			dbc.setString(2, zuzhi.getJingptu());
			dbc.setString(3, zuzhi.getJingpNeirong());
			dbc.setInt(4, id);
			
			dbc.executeUpdate();
			return true;
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}finally{
			dbc.close();
		}
	}
	
	
	/**
	 * 列表返回显示总结
	 * 
	 */
	
	public Collection GetZuzhi()
	{
		DBConnect dbc = new DBConnect();
		String sql = "select * from zuzhi order by jingpId desc";
		dbc.prepareStatement(sql);
		Collection<Zuzhi> zuzhiList = new ArrayList<Zuzhi>();
		try {
			ResultSet rs = dbc.executeQuery();
			while(rs.next())
			{
				
				Zuzhi  zuzhi = new Zuzhi();
				zuzhi.setJingpId(rs.getInt("jingpId"));
				zuzhi.setJingpName(rs.getString("jingpName"));
				zuzhi.setJingptu(rs.getString("jingptu"));
				zuzhi.setJingpNeirong(rs.getString("jingpNeirong"));
			}
			return zuzhiList;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			dbc.close();
		}
		
		return null;
	}
	


	/*
	 * 返回总结列表，分页显示
	 */
	
	public Collection GetZuzhi(int page, int rowsPerPage)
	{
		String sql = "select * from zuzhi order by jingpId desc";
		pageInstance = new ZuzhiControlPage(sql,rowsPerPage);
		
		try {
			return pageInstance.getPage(page);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;

	}
	

class ZuzhiControlPage extends Pagination{
	public ZuzhiControlPage(String sql, int rowsPerPage)
	{
		super(sql,rowsPerPage,1);
	}
	
	@Override
	protected Collection<Zuzhi> packResultSet(ResultSet rs) throws SQLException {
		// TODO Auto-generated method stub
		Collection<Zuzhi>zuzhiList = new ArrayList<Zuzhi>();
		while(rs.next())
		{
			Zuzhi zuzhi = new Zuzhi();
			
			zuzhi.setJingpId(rs.getInt("jingpId"));	
			zuzhi.setJingpName(rs.getString("jingpName"));
			zuzhi.setJingptu(rs.getString("jingptu"));
			zuzhi.setJingpNeirong(rs.getString("jingpNeirong"));
			
			zuzhiList.add( zuzhi);
			zuzhi = null;
			}
			return  zuzhiList;
			
		}
	
	}
	
}
	


	
	
	

	

	
	
